/**
  ******************************************************************************
  * @file    adc.c
  * @author  测试1组（houtiezhu）
  * @version v0
  * @date    09-07-2019
  *
  * @verbatim
  **/
/* Includes ------------------------------------------------------------------*/

#include "define.h"
#include "disp.h"
#include "power.h"
#include "clk.h"
#include "gpio.h"
#include "time.h"
#include "uart.h"
#include "spk.h"
#include "flash.h"
#include "adc.h"
#include "algo.h"
#include "bp.h"
#include "func.h"
#include "op.h"
#include "pwm.h"
#include "voice.h"
#include "queue.h"
#include "inner_voice.h"

uint16 BatValue;



void UserSDADCInit(void)
{

	SDADC_DeInit();
	RCC_APBPeriphClockCmd(RCC_APBPeriph_SDADC,ENABLE);   //ADC时钟源使能

	IA_InitTypeDef	SDADC_IAInitStructure;
//PGIA初始化
	SDADC_IAInitStructure.IA_ExteralChopper=IA_ExteralChopper_OFF;//关闭PGIA外部chopper
	SDADC_IAInitStructure.IA_InteralChopper=IA_InteralChopper_DIV128;//内部chopper选择fs/128
	SDADC_IAInitStructure.IA_GAIN=IA_GAIN16;//32倍增益IA_GAIN32
//2023年4月10日 移植待确认项 IA_CR寄存器中IAD（PGIA 偏置电流选择控制位）由1位增加为2位。
	SDADC_IAInitStructure.IA_IAD=IA_IAD_x2;//
//2023年4月10日 移植待确认项 新增IA_CR2寄存器，新增PGIA 输入端面积选择控制位。
	SDADC_IAInitStructure.IA_SIA=IA_SIA_HIGH;      //  PGIA输入端面积选择控制位


	SDADC_IAInitStructure.IA_OSEN=DISABLE;//不使能VOS模块
	SDADC_IAInitStructure.IA_OSSIGN=SET;//VOS为正值
	SDADC_IAInitStructure.IA_VOS=0x0f;//VOS大小设置
	SDADC_IAInitStructure.IA_RCF=IA_RCF_20k;//RC滤波器中R选择20k
	SDADC_IAInitStructure.IA_SIEN=DISABLE;//IAINP、IAINN不短接
	SDADC_IAInitStructure.SDADC_IAINNS=SDADC_IAINNS_A1;//IA的输入信号负端选择A1
	SDADC_IAInitStructure.SDADC_IAINPS=SDADC_IAINPS_A0;//IA的输入信号正端选择A0
	SDADC_IAInit(&SDADC_IAInitStructure);
	SDADC_PGIACmd(ENABLE);//PGIA使能

//SDADC初始化
	SDADC_InitTypeDef	SDADC_InitStructure;
	SDADC_InitStructure.SDADC_ADOUTS=SET;//选择SINC3滤波器
	SDADC_InitStructure.SDADC_ASPCK1=512000;//750kHz
	SDADC_InitStructure.SDADC_BUFFDS=SDADC_BUFFDS64;//Buf1/Buf2 chopper频率fb设置为fs/64
	SDADC_InitStructure.SDADC_DCSET=SDADC_DCSET0;//ADC失调电压选择0
	SDADC_InitStructure.SDADC_DIEN=ENABLE;//使能ADC的dither
	SDADC_InitStructure.SDADC_OSR=SDADC_OSR8192;//OSR选择  750000/8192=91Hz
	SDADC_InitStructure.SDADC_PAEN=DISABLE;//不使用ADC的外部chopper
	SDADC_InitStructure.SDADC_VREFG=SDADC_VREFG1_2;//ADC参考电压放大倍数选择1/2
	SDADC_InitStructure.SDADC_VREFS=SDADC_VREFS_AVDDRVSS;//ADC参考电压源选择AVDDR-VSS    AVDDR=2.4V
	SDADC_Init(&SDADC_InitStructure);
	SDADC_Buf2Cmd(ENABLE);
	SDADC_Buf1Cmd(ENABLE);
	SDADC_P2ENCmd(ENABLE);
	SDADC_Cmd(ENABLE);//SDADC使能

}



//Vref =AVDDR=2.4V
void UserSARADC_LBT_Init(void)
{
	SARADC_InitTypeDef  SARADC_InitStruct;
	SARADC_StopConversionCmd(ENABLE);
	SARADC_Cmd(DISABLE);
	SARADC_DeInit();
	//RCC_APBPeriphResetCmd(RCC_APBPeriph_SARADC, ENABLE);
	RCC_APBPeriphClockCmd(RCC_APBPeriph_SARADC,ENABLE);

	SARADC_InitStruct.SARADC_BufMode = SARADC_BufMode_LowSpeed;       //SAR Buf高低速模式及电流选择位SARADC_BufMode_HighSpeed
	SARADC_InitStruct.SARADC_BufPhase = SARADC_BufPhase_P;     //SAR Buf相位选择位
	SARADC_InitStruct.SARADC_BufEN = DISABLE;       //SAR Buf使能位
	SARADC_InitStruct.SARADC_Channel = SARADC_Channel_A2;     //SARADC输入信号选择位 20201102 A3 to A2
	SARADC_InitStruct.SARADC_SARCLK = SARADC_SARCLK_IHRC_DIV96;       //SAR ADC采样时钟SAR_CLK选择位
	SARADC_InitStruct.SARADC_ReadyTime = SARADC_ReadyTime_128SARCLK;   //SAR ADC准备时间选择位
	SARADC_InitStruct.SARADC_SampleTime = SARADC_SampleTime_4SARCLK; //SAR ADC转换时信号采样周期数选择位
    SARADC_InitStruct.SARADC_ContinuousMode = ENABLE;      //转换模式选择位		//20211011
    SARADC_InitStruct.SARADC_WaitMode = DISABLE;	        //wait模式使能位s
	SARADC_InitStruct.SARADC_OVR_LoseCurrentValue = DISABLE;//发生OVR错误时是否保留当前数据 ENABLE
	SARADC_InitStruct.SARADC_HighSpeedMode = ENABLE;	    //SAR ADC高低速模式及电流选择位

	SARADC_Init(&SARADC_InitStruct);
	SARADC_Cmd(ENABLE);
	while(SARADC_GetITStatus(SARADC_SR_RDYIF) == 0);//准备完成等待判断
	SARADC_ClearITStatus(SARADC_SR_RDYIF);//清准备完成标志位
	SARADC_ConversionCmd(ENABLE);
}


void UserSARADC_Temp_Init(void)
{
	SARADC_InitTypeDef  SARADC_InitStruct;
	SARADC_StopConversionCmd(ENABLE);
	SARADC_Cmd(DISABLE);
	SARADC_DeInit();
	RCC_APBPeriphClockCmd(RCC_APBPeriph_SARADC,ENABLE);

	SARADC_InitStruct.SARADC_BufMode = SARADC_BufMode_LowSpeed;       //SAR Buf高低速模式及电流选择位SARADC_BufMode_HighSpeed
	SARADC_InitStruct.SARADC_BufPhase = SARADC_BufPhase_P;     //SAR Buf相位选择位
	SARADC_InitStruct.SARADC_BufEN = DISABLE;       //SAR Buf使能位
	SARADC_InitStruct.SARADC_Channel = SARADC_Channel_A3;     //SARADC输入信号选择位	20201102 A2 to A3

	SARADC_InitStruct.SARADC_SARCLK = SARADC_SARCLK_IHRC_DIV96;       //SAR ADC采样时钟SAR_CLK选择位
	SARADC_InitStruct.SARADC_ReadyTime = SARADC_ReadyTime_128SARCLK;   //SAR ADC准备时间选择位
	SARADC_InitStruct.SARADC_SampleTime = SARADC_SampleTime_4SARCLK; //SAR ADC转换时信号采样周期数选择位
    SARADC_InitStruct.SARADC_ContinuousMode = DISABLE;      //转换模式选择位
    SARADC_InitStruct.SARADC_WaitMode = DISABLE;	        //wait模式使能位
	SARADC_InitStruct.SARADC_OVR_LoseCurrentValue = DISABLE;//发生OVR错误时是否保留当前数据 ENABLE
	SARADC_InitStruct.SARADC_HighSpeedMode = ENABLE;	    //SAR ADC高低速模式及电流选择位

	SARADC_Init(&SARADC_InitStruct);
	SARADC_Cmd(ENABLE);
	while(SARADC_GetITStatus(SARADC_SR_RDYIF) == 0);//准备完成等待判断
	SARADC_ClearITStatus(SARADC_SR_RDYIF);//清准备完成标志位
}






//==========================================================================================
//AVDDR=3.0V
//4.4V=950
//4.5V=1000
//4.6V=1050
//3.4V=780
//3.5V=
//SRADC LBT
//--------------------------------------------
//AVDDR=2.4V
//4.25V=11000
//
//==========================================================================================
//2023年4月10日 507只有10位SARADC  取值范围是0~1024
//所以阈值对应要缩小4倍
//(vbat = 4.2v && R4 = 499 && R5 = 100) 时 阈值大概为 282
#define	LBTADCVAL   1175		//20210528
uint32  LbtTemp;
uint16	LBTCount;
uint16  LbtTable[4];
void CheckBatteryCOM(void)
{
	uint8 k;
	//uint16  LbtTemp;
	//LBTCount=0;
//	UserSARADC_LBT_Init();
	//delay(8);
	LbtTemp = 0;

	LbtTable[3]=LbtTable[2];
	LbtTable[2]=LbtTable[1];
	LbtTable[1]=LbtTable[0];
	LbtTable[0]=SARADC_GetConversionValue();
	LBTCount++;
	if(LBTCount>1000) LBTCount=1000;
	for(k=0;k<4;k++)
	{
		LbtTemp+=LbtTable[k];
	}

	LbtTemp =LbtTemp>>2;

//	LCD5bitDisplayT(LbtTemp);		//20211009 debug

	if(LBTCount>4 && LbtTemp < LBTADCVAL / 4)
		goto ShowLOWBAT2; //2023年4月10日
	else goto HighBAT2;


ShowLOWBAT2:
	while(!KEY_POWER);
	keydelay();
	while(!KEY_POWER);
	DarkAllIcon();
	//if(ConnectFlag&&BTIconEnable) BTIconOn;
//	if(!NoTimer){showtime();showdate();TimePointIconOn;}  //20210723 delete
//	BUZZER_DIS;            	//20211103 delete
	BB= VALVEOFF  | PUMPOFF;
	iocontroloff();
	lowbsignon();
	AutoTestFlag=0;
	BPTestFlag=0;
	BTFlag=0;
	SendMemoryFlag=0;
	ResultCode=0x0B;
	if(UartType==1||UartType==4||UartType==5||UartType==6)
	{
		Send_LowBattery();
		RXIEnable;
		BTFlag=0;
	}

	AutoOffTimew=0x0000;
 	if(VoiceEnable==1)
	{
		if(VoiceType==1||VoiceType==10||VoiceType==14)
		{
            if (VoiceType==1 && VoiceType2 == 4)//BP507内置语音
            {
                VoiceStop = 0;
                inner_voice_one_cmd(INNER_CMD_INSUFFICIENT_POWER);
                if (VoiceStop == 0)
                {
                    inner_voice_one_cmd(INNER_CMD_PLEASE_REPLACE_THE_BATTERY);
                }
            }
            else
            {
                if(PowerType) SoundAllCommand(0x22,0,0,0);	//锂电，请及时充电
                else SoundAllCommand(0x11,0,0,0);		//电力不足,请更换电池
            }
		}
		else if(VoiceType==2)
		{
			if(PowerType) SoundCommand(0xfe);	//no have voice
			else SoundCommand(0x91);		//battery is Low , please replace the batteries
		}
		else if(VoiceType>2&&VoiceType<=4)
		{
			SoundCommand2(0x91);
		}
		else if(VoiceType==5)
		{
			SoundAllCommand(0x33,0,0,0);
		}
		else if(VoiceType==7)
		{
			SoundCommand(0xC1);
		}
		else if(VoiceType==6)
		{
			SoundAllCommand2(0x11,0,0,0);
		}
		else if(VoiceType==8&&VoiceFlag<5)
		{
			SoundFive();
			SoundCommand2(0xd1);
		}
		else if(VoiceType==9)
		{
			SoundAllCommand(0x11,0,0,0);
		}
		else if(VoiceType==11)
		{
			SoundCommand2(0xd2);
		}
		else if(VoiceType==12)
		{
			SoundAllCommand(0x11,0,0,0);
		}
		else if(VoiceType==13&&VoiceLevel>0)
		{
			SoundThree();
			SoundCommand2(0xD0);
		}
		else if (VoiceType == 15)
        {
            new_cn_ic_voice_send_cmd_server(VOICE_CMD_INSUFFICIENT_POWER);
        }
	}
	KeyModeFlag.b=0;
	do{
			delay(1);
			if(UartType==5&&WorkMode==1) break;
			if(!AllTimeFlag)
			{
				if(ModifyFlag)
				{
					if(AutoOffTimew>AUTOOFFTIME) break;
				}
				else
				{
					if(AutoOffTime2b>=0x2) break;
				}
			}

			if(KeyNumber==11)
			{
				if(!KEY_MEMORYUP||!KEY_MEMORYDOWN) {MemoryKeyFlag=1;break;}
				if(LongSetFlag) {SetKeyFlag=1;break;}
			}
			if(KeyNumber==12)
			{
				if(!KEY_TIME) {MemoryKeyFlag=1;break;}
			}
	}while(KEY_POWER&&!BTFlag&&!SendMemoryFlag); //AUTOOFFTIME
//	BB|= POWEROFF;
//	iocontroloff();
	IsPowerOn=0;
	DarkAllIcon();
HighBAT2:
//	BUZZER_DIS;		//20211103 delete
	nop4();
}


void CheckBatteryADC(void)
{
	unsigned int LBTValue=0;
	//LBTCount=0;
	TemplateValueLong = 0;
	UserSARADC_LBT_Init();
	delay(5);
	SARADC_ConversionCmd(ENABLE);//启动转换
	SARADC_ClearITStatus(SARADC_SR_CONIF);//清转换完成标志位
    while(!SARADC_GetITStatus(SARADC_SR_CONIF));//SARADC_SR_CONIF

	TemplateValueLong=SARADC_GetConversionValue();

	TemplateValueLong*=792;
	TemplateValueLong/=32768;
	LBTValue = TemplateValueLong;

	BatValue=LBTValue;		//线性阀
	if(BatValue>620) BatValue=620;
	if(BatValue<400) BatValue=400;

}






